import { request } from '../request';

/** 获取映射集列表 */
export function fetchModelSetList(params?: Api.ProductModelMapping.MappingSetListParams) {
  return request<Api.ProductModelMapping.MappingSetListResponse>({
    url: '/api/valuation/product-model-relations/mapping-sets',
    method: 'get',
    params
  });
}

/** 获取映射集列表（兼容旧命名） */
export function fetchMappingSetList(params?: Api.ProductModelMapping.MappingSetListParams) {
  // 兼容旧代码的命名，内部复用新命名函数
  return fetchModelSetList(params);
}

/** 获取映射集详情 */
export function fetchMappingSetDetail(id: string) {
  return request<Api.ProductModelMapping.MappingSet>({
    url: `/api/valuation/product-model-relations/mapping-sets/${id}`,
    method: 'get'
  });
}

/** 创建映射集 */
export function createMappingSet(data: Api.ProductModelMapping.CreateMappingSetParams) {
  return request<Api.ProductModelMapping.MappingSet>({
    url: '/api/valuation/product-model-relations/mapping-sets',
    method: 'post',
    data
  });
}

/** 更新映射集 */
export function updateMappingSet(data: Api.ProductModelMapping.UpdateMappingSetParams) {
  return request<Api.ProductModelMapping.MappingSet>({
    url: `/api/valuation/product-model-relations/mapping-sets/${data.id}`,
    method: 'put',
    data
  });
}

/** 删除映射集 */
export function deleteMappingSet(id: string) {
  return request<boolean>({
    url: `/api/valuation/datasets/${id}`,
    method: 'delete'
  });
}

/** 批量删除映射集 */
export function batchDeleteMappingSets(ids: string[]) {
  return request<boolean>({
    url: '/api/valuation/product-model-relations/mapping-sets/batch-delete',
    method: 'post',
    data: { ids }
  });
}

/** 获取映射集的统计信息 */
export function fetchMappingSetStats(id: string) {
  return request<Api.ProductModelMapping.MappingSetStats>({
    url: `/api/valuation/product-model-relations/mapping-sets/${id}/stats`,
    method: 'get'
  });
}

/** 获取指定映射集的产品-模型映射关系 */
export function fetchMappingsBySet(params: Api.ProductModelMapping.MappingsBySetParams) {
  return request<Api.ProductModelMapping.ProductModelRelationWithSet[]>({
    url: `/api/valuation/product-model-relations/mappings`,
    method: 'get',
    params
  });
}

/** 在指定映射集中创建产品-模型映射关系 */
export function createMappingInSet(data: Api.ProductModelMapping.CreateMappingParams) {
  return request<Api.ProductModelMapping.ProductModelRelationWithSet>({
    url: '/api/valuation/product-model-relations/mappings',
    method: 'post',
    data
  });
}

/** 批量创建产品-模型映射关系 */
export function batchCreateMappingsInSet(data: Api.ProductModelMapping.BatchCreateMappingParams) {
  return request<Api.ProductModelMapping.ProductModelRelationWithSet[]>({
    url: '/api/valuation/product-model-relations/mappings/batch',
    method: 'post',
    data
  });
}

/** 删除指定映射集中的产品-模型映射关系 */
export function deleteMappingInSet(data: Api.ProductModelMapping.DeleteMappingParams) {
  return request<boolean>({
    url: '/api/valuation/product-model-relations/mappings',
    method: 'delete',
    data
  });
}

/** 复制映射集 */
export function copyMappingSet(sourceId: string, targetName: string, targetDescription?: string) {
  return request<Api.ProductModelMapping.MappingSet>({
    url: `/api/valuation/product-model-relations/mapping-sets/${sourceId}/copy`,
    method: 'post',
    data: {
      name: targetName,
      description: targetDescription
    }
  });
}

/** 导出映射集配置 */
export function exportMappingSet(id: string) {
  return request({
    url: `/api/valuation/product-model-relations/mapping-sets/${id}/export`,
    method: 'get',
    responseType: 'blob'
  });
}

/** 导入映射集配置 */
export function importMappingSet(file: File, name: string, description?: string) {
  const formData = new FormData();
  formData.append('file', file);
  formData.append('name', name);
  if (description) {
    formData.append('description', description);
  }

  return request<Api.ProductModelMapping.MappingSet>({
    url: '/api/valuation/product-model-relations/mapping-sets/import',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  });
}

// 产品模型关联关系API
export function fetchProductModelMappings(productId: number) {
  return request<Api.Valuation.ProductModelRelation[]>({
    url: `/api/valuation/product-model-relations/product/${productId}`,
    method: 'get'
  });
}

// 根据映射集ID和产品ID获取产品模型映射关系
export function fetchProductModelMappingsByDataSetId(dataSetId: string, productId: number) {
  return request<Api.Valuation.ProductModelRelation[]>({
    url: `/api/valuation/product-model-relations/dataset/${dataSetId}/product/${productId}`,
    method: 'get'
  });
}

// 根据映射集ID获取所有产品模型映射关系
export function fetchAllMappingsByDataSetId(dataSetId: string) {
  return request<Api.Valuation.ProductModelRelation[]>({
    url: `/api/valuation/product-model-relations/dataset/${dataSetId}`,
    method: 'get'
  });
}

export function createProductModelMapping(data: {
  productId: number;
  modelId: number;
  isDefault?: boolean;
  configParams?: string;
}) {
  return request<Api.Valuation.ProductModelRelation>({
    url: '/api/valuation/product-model-relations',
    method: 'post',
    data
  });
}

// 创建产品模型映射关系（包含映射集ID）
export function createProductModelMappingWithDataSet(data: {
  productId: number;
  modelId: number;
  dataSetId: string;
  isDefault?: boolean;
  configParams?: string;
}) {
  return request<Api.Valuation.ProductModelRelation>({
    url: '/api/valuation/product-model-relations',
    method: 'post',
    data
  });
}

export function updateProductModelMapping(
  id: number,
  data: {
    isDefault?: boolean;
    configParams?: string;
  }
) {
  return request<Api.Valuation.ProductModelRelation>({
    url: `/api/valuation/product-model-relations/${id}`,
    method: 'put',
    data
  });
}

export function deleteProductModelMapping(id: number) {
  return request({
    url: `/api/valuation/product-model-relations/${id}`,
    method: 'delete'
  });
}

export function setDefaultModel(productId: number, modelId: number) {
  return request({
    url: `/api/valuation/product-model-relations/set-default`,
    method: 'put',
    params: { productId, modelId }
  });
}

// 产品定义扫描API
export function fettchProductDefinitions() {
  return request<Api.Valuation.Product[]>({
    url: '/api/valuation/product-definition/fetch',
    method: 'get'
  });
}

export function getProductDefinitionCategories() {
  return request<Record<string, Api.Valuation.ProductDefinition[]>>({
    url: '/api/valuation/product-definition/categories',
    method: 'get'
  });
}
